package com.woniu.user.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

import com.woniu.user.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.woniu.user.mapper.DoctorMapper;
import com.woniu.user.service.IDoctorService;

import javax.annotation.Resource;

/**
 * 工作人员表Service业务层处理
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@Service
public class DoctorServiceImpl implements IDoctorService{

    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private RedisTemplate redisTemplate;

    private  int week=0;

    /**
     * 查询工作人员表
     *
     * @param doctorId 工作人员表主键
     * @return 工作人员表
     */
    @Override
    public Doctor selectDoctorByDoctorId(Long doctorId) {
        return doctorMapper.selectDoctorByDoctorId(doctorId);
    }

    @Override
    public Doctor selectDoctorById(Long doctorId) {
        return doctorMapper.selectDoctorById(doctorId);
    }

    /**
     * 查询工作人员表列表
     *
     * @param doctor 工作人员表
     * @return 工作人员表
     */
    @Override
    public List<Doctor> selectDoctorList(Doctor doctor) {
        return doctorMapper.selectDoctorList(doctor);
    }

    @Override
    public Doctor getDoctorIdByName(String doctorName) {

        Doctor doctorIdByName = doctorMapper.getDoctorIdByName(doctorName);
        return doctorIdByName;
    }

    @Override
    public String getDoctorNameById(Long doctorId) {
        return doctorMapper.getDoctorNameById(doctorId);
    }

    /**
     * 新增工作人员表
     *
     * @param doctor 工作人员表
     * @return 结果
     */
    @Override
    public int insertDoctor(Doctor doctor) {
        return doctorMapper.insertDoctor(doctor);
    }

    /**
     * 修改工作人员表
     *
     * @param doctor 工作人员表
     * @return 结果
     */
    @Override
    public int updateDoctor(Doctor doctor) {
        return doctorMapper.updateDoctor(doctor);
    }

    /**
     * 批量删除工作人员表
     *
     * @param doctorIds 需要删除的工作人员表主键
     * @return 结果
     */
    @Override
    public int deleteDoctorByDoctorIds(Long[] doctorIds) {
        return doctorMapper.deleteDoctorByDoctorIds(doctorIds);
    }

    /**
     * 删除工作人员表信息
     *
     * @param doctorId 工作人员表主键
     * @return 结果
     */
    @Override
    public int deleteDoctorByDoctorId(Long doctorId) {
        return doctorMapper.deleteDoctorByDoctorId(doctorId);
    }

    @Override
    public List<SmallDoctorVo> getDoctorList() {
        return doctorMapper.getDoctorList();
    }

    @Override
    public DeptAndDoctorName getDoctorNameAndDeptName(Long doctorId) {
        return doctorMapper.getDoctorNameAndDeptName(doctorId);
    }


    @Override
    public Doctor selectById(long id) {
        return doctorMapper.selectById(id);
    }

    @Override
    public List<Doctor> filtrate(long deptId, long roleId) {
        return doctorMapper.filtrate(deptId, roleId);
    }
    @Override
    public List<Doctor> getDoctorLikeLists() {
        return doctorMapper.getDoctorLikeLists();
    }


    public static HashMap doctorIn(){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= 7; i++) {
            map.put(i,i);
        }
        return  map;
    }



    //排班系统(搜索的情况下)
    public   List<paibanVo> paibanEnd2(String date,String time,int id){
        //判断时间是当年第几周
        String weeks=weeks(time);
        //判断date是周几
        int weeknum = StringToWeek(date);
      return flag(id, weeknum, weeks);
    }

    //排班系统(当前页面情况)
    public   List<paibanVo> paibanEnd(String date,int id){


        //判断时间是当年第几周
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        week = calendar.get(Calendar.WEEK_OF_YEAR);
        String weeks=week+"";

        //判断date是周几
        int weeknum = StringToWeek(date);
        return flag(id, weeknum, weeks);
    }

    //判断方法
    public List<paibanVo> flag(int id,int weeknum,String  weeks){
        //判断redis是不是空
        HashMap<Integer,ArrayList<Integer>> flagResult = (HashMap<Integer,ArrayList<Integer>>)redisTemplate.opsForValue().get(weeks);

        //redis不为空
        if(flagResult!=null)
        {
            if(id!=0){  //传入id 就减值，返回redis数据
                ArrayList<Integer> integers = flagResult.get(weeknum);
                int i = integers.indexOf(id);
                System.out.println(integers+"in");
                integers.set(i+1,(integers.get(i+1)-1));
                System.out.println(integers+"out");
                flagResult.put(weeknum,integers);
                redisTemplate.opsForValue().set(weeks,flagResult);
                return  doctorName(flagResult);
            }else {   //没id就返回redis数据
                return  doctorName(flagResult);
            }
        }
        else  //redis为空,执行排班放入redis,返回数据
        {
            HashMap doctors = doctorIn();
            HashMap<Integer, ArrayList<Integer>> paiban = paiban(doctors);

            redisTemplate.opsForValue().set(weeks,paiban);
            List<paibanVo> paibanVo = doctorName(paiban);
            return paibanVo;
        }
    }




    public   List<paibanVo>  doctorName(HashMap<Integer,ArrayList<Integer>> result){
        System.out.println("result = " + result);
         List<paibanVo> jieguo=new ArrayList<>();
        for (int i = 1; i < result.size(); i++){
            paibanVo paibanVo = new paibanVo();
            paibanVo.setDateName(weekString(i));
            for (int j = 0; j <result.get(i).size(); j+=2) {
                Integer doctorId = result.get(i).get(j);
                long l = Long.parseLong(String.valueOf(doctorId));
                DeptAndDoctorName doctorNameAndDeptName = doctorMapper.getDoctorNameAndDeptName(l);
                switch (j){
                    case 0:
                        paibanVo.setDoctor1(doctorNameAndDeptName.getDoctorName()+"("+doctorNameAndDeptName.getDeptName()+"):余号:"+result.get(i).get(j+1));
                        paibanVo.setDoctor1ID(Integer.parseInt(String.valueOf(l)));
                        break;
                    case 2:
                        paibanVo.setDoctor2(doctorNameAndDeptName.getDoctorName()+"("+doctorNameAndDeptName.getDeptName()+"):余号:"+result.get(i).get(j+1));
                        paibanVo.setDoctor2ID(Integer.parseInt(String.valueOf(l)));
                        break;
                    case 4:
                        paibanVo.setDoctor3(doctorNameAndDeptName.getDoctorName()+"("+doctorNameAndDeptName.getDeptName()+"):余号:"+result.get(i).get(j+1));
                        paibanVo.setDoctor3ID(Integer.parseInt(String.valueOf(l)));
                        break;
                    case 6:
                        paibanVo.setDoctor4(doctorNameAndDeptName.getDoctorName()+"("+doctorNameAndDeptName.getDeptName()+"):余号:"+result.get(i).get(j+1));
                        paibanVo.setDoctor4ID(Integer.parseInt(String.valueOf(l)));
                        break;
                }
            }
            jieguo.add(paibanVo);
        }
        return jieguo;
    }
    public int weekNums(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<=0){
            week_index = 7;
        }
        return week_index;
    }

    @Override
    public List<Doctor> getDoctorListYan() {
        Doctor doctor = new Doctor();
        doctor.setDeptId(1L);
        return doctorMapper.selectDoctorList(doctor);
    }


    public static HashMap<Integer,ArrayList<Integer>> paiban(HashMap doctors){
        HashMap<Integer,ArrayList<Integer>> maps = new HashMap<>();
        HashMap<Integer,Integer> mapMax = new HashMap<>();
        for (int i = 1; i <= doctors.size(); i++) {
            mapMax.put(i,0);
        }
        for (int i = 1; i <= 7; i++) {
            int j=1;
            while (j<5){
                Random rand = new Random();
                int random=rand.nextInt(7)+1;//[1,7]内的随机整数
                if(mapMax.get(random) < 4){
                    ArrayList<Integer> integers = maps.get(i);
                    if (integers != null && integers.contains(random)&& i!=7) {
                    } else {
                        if(integers == null) {
                            integers = new ArrayList<Integer>();
                        }
                        mapMax.put(random, mapMax.get(random) + 1);
                        integers.add(random);
                        integers.add(40);
                        maps.put(i, integers);
                        j++;
                    }
                }
            }
        }
        return maps;
    }

    public String weeks(String date){
        String weeks=null;
        //判断时间是当年第几周
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date parse1 = null;
        try {
            parse1= simpleDateFormat1.parse(date);
        }catch (Exception e){}
        calendar.setTime(parse1);
        week = calendar.get(Calendar.WEEK_OF_YEAR);
        weeks=week+"";

        return weeks;
    }

    public int weekNum(String date){
        int weeknum=0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse= simpleDateFormat.parse(date);
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
            String  weekString = sdf.format(parse);
            weeknum = StringToWeek(weekString);
        }catch (Exception e){}
        return weeknum;
    }

    public String weekString(int i){
        String str=null;
        switch (i){
            case 1:
                str="星期一";
                break;
            case 2:
                str="星期二";
                break;
            case 3:
                str="星期三";
                break;
            case 4:
                str="星期四";
                break;
            case 5:
                str="星期五";
                break;
            case 6:
                str="星期六";
                break;
            case 7:
                str="星期日";
                break;
        }
        return str;
    }
    public int StringToWeek(String week){
        int str=0;
        switch (week){
            case "星期一":
                str=1;
                break;
            case "星期二":
                str=2;
                break;
            case "星期三":
                str=3;
                break;
            case "星期四":
                str=4;
                break;
            case "星期五":
                str=5;
                break;
            case "星期六":
                str=6;
                break;
            case "星期日":
                str=7;
                break;
            case "当日":
                str=weekNums(new Date());
                break;
        }
        return str;
    }

}
